
import { useStore } from '@/store'
const loginIgnore = {
  names: ['404', '401'],
  paths: ['/login'],
  includes(route) {
    return this.names.includes(route.name) || this.paths.includes(route.path)
  }
}
// 加载路由守卫
export function loadGuards(router) {
  router.beforeEach(async (to, from, next) => {
    if (to.meta.title) {
      document.title = ' 品客管理后台--' + to.meta.title
    }
    if(loginIgnore.includes(to)){
      next()
    } else if (localStorage.getItem('token')) {
      if (to.name == 'Login') {
        next({ path: '/login' })
      } else {
        const hasRoute = router.hasRoute(to.name)
        if (hasRoute) {
          const store = useStore()
          if (store.getters.userInfo && !store.getters.userInfo.user_id) {
            try {
              await store.dispatch('app/getInfo')
            } catch (error) {
              await store.dispatch('tagsView/resetProcess')
              console.log(error)
            }
          }
          await store.commit('tagsView/addProcess', {
            noCache: to.meta.noCache,
            label: to.meta.title,
            value: to.fullPath,
            name: to.name
          })
          next()
        } else {
          next({ ...to, replace: true })
        }
      }
    } else {
      if (!loginIgnore.includes(to)) {
        next({
          path: "/login",
          replace: true
        })
      } else {
        next()
      }
    }
  })
}
